package com.pro.core.validator;

import com.baidu.unbiz.fluentvalidator.ValidationError;
import com.baidu.unbiz.fluentvalidator.Validator;
import com.baidu.unbiz.fluentvalidator.ValidatorContext;
import com.baidu.unbiz.fluentvalidator.ValidatorHandler;

/**
 * 校验 4<orderNo>64
 */
public class OrderNoLengthValidator extends ValidatorHandler<Object> implements Validator<Object> {

    private String fieldName;

    public OrderNoLengthValidator(String fieldName) {
        this.fieldName = fieldName;
    }

    @Override
    public boolean validate(ValidatorContext context, Object s) {
    	if (null == s || s.equals("") || s.equals("null")) {
    		context.addError(ValidationError.create(String.format("%s不能为空！", fieldName))
                    .setErrorCode(-1)
                    .setField(fieldName)
                    .setInvalidValue(s));
            return false;
		}
        if (s instanceof String) {
        	if (((String) s).length()>4&&((String) s).length()<64) {
        		return true;
			}
        }
        context.addError(ValidationError.create(String.format("%s长度错误！", fieldName))
                .setErrorCode(-1)
                .setField(fieldName)
                .setInvalidValue(s));
        return false;
    }

}
